<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片搜索 -->
    <el-card class="box-card">
      <el-input
        placeholder="请输入内容"
        v-model="queryinfo.query"
        clearable
        class="serch"
      ></el-input>
      <el-button icon="el-icon-search" border="0" @click="getOrders"
        >搜索</el-button
      >
    </el-card>
    <!-- 页面渲染 -->
    <el-table :data="orderData" height="400" border style="width: 100%">
      <el-table-column type="index" width="50" label="#"> </el-table-column>
      <el-table-column prop="order_id" label="订单ID" width="100">
      </el-table-column>
      <el-table-column prop="order_number" label="订单编号" width="280">
      </el-table-column>
      <el-table-column prop="order_price" label="订单价格" width="150">
      </el-table-column>
      <el-table-column prop="pay_status" label="是否付款" width="150">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.pay_status === '1'"
            >已付款</el-tag
          >
          <el-tag type="danger" v-else-if="scope.row.pay_status === '0'"
            >未付款</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="is_send" label="是否发货" width="150">
      </el-table-column>
      <el-table-column prop="create_time" label="下单时间" width="200">
      <!-- 格式化时间 -->
        <template slot-scope="scope">
          {{ scope.row.create_time | newFromTime }}
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="EditorOrders(scope.row)"
            >编辑</el-button
          >
          <el-button
            type="success"
            icon="el-icon-search"
            size="mini"
            @click="QueryOrders(scope.row)"
            >查询</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页按钮 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryinfo.pagenum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="queryinfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- 编辑订单弹窗 -->
    <!-- 编辑商品信息 -->
    <el-dialog title="修改订单" :visible.sync="dialogChangeOrders">
      <el-form :model="changeOrderData">
        <el-form-item label="订单ID">
          <el-input
            v-model="changeOrderData.order_id"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单编号">
          <el-input
            v-model="changeOrderData.order_number"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单支付">
          <el-input
            v-model="changeOrderData.order_pay"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单价格">
          <el-input
            v-model="changeOrderData.order_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="支付状态">
          <el-input
            v-model="changeOrderData.pay_status"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-radio v-model="changeOrderData.is_send" label="1">是</el-radio>
          <el-radio v-model="changeOrderData.is_send" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogChangeOrders = false">取 消</el-button>
        <el-button type="primary" @click="okChangeOrders">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 查询详情 -->
    <el-dialog title="查询详情" :visible.sync="dialogqueryOrders">
      <el-form :model="QueryOrdersData">
        <el-form-item label="订单ID">
          <el-input
            v-model="QueryOrdersData.order_id"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单编号">
          <el-input
            v-model="QueryOrdersData.order_number"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单支付">
          <el-input
            v-model="QueryOrdersData.order_pay"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="订单价格">
          <el-input
            v-model="QueryOrdersData.order_price"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="支付状态">
          <el-input
            v-model="QueryOrdersData.pay_status"
            autocomplete="off"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-radio v-model="QueryOrdersData.is_send" label="1">是</el-radio>
          <el-radio v-model="QueryOrdersData.is_send" label="0">否</el-radio>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogqueryOrders = false">取 消</el-button>
        <el-button type="primary" @click="dialogqueryOrders = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "VueOrders",

  data() {
    return {
      //渲染参数
      queryinfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      //遍历数组
      orderData: [],
      //总数
      total: 0,
      //修改订单弹窗
      dialogChangeOrders: false,
      //修改存储
      changeOrderData: {},
      //查询弹窗
      dialogqueryOrders: false,
      //查询储存
      QueryOrdersData: {},
    };
  },

  methods: {
    async getOrders() {
      //发送请求
      var { data } = await this.$axios.get("orders", {
        params: this.queryinfo,
      });
      //   console.log(data);
      if (data.meta.status == 200) {
        this.orderData = data.data.goods;
        this.total = data.data.total;
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    //pageSize 改变时会触发
    handleSizeChange(newsize) {
      this.queryinfo.pagesize = newsize;
      this.getOrders();
    },
    // currentPage 改变时会触发
    handleCurrentChange(newpage) {
      //页面发生变化时触发
      this.queryinfo.pagenum = newpage;
      this.getOrders();
    },
    //编辑
    EditorOrders(orderinfo) {
      this.dialogChangeOrders = true;
      this.changeOrderData = orderinfo;
    },
    //确认编辑
    async okChangeOrders() {
      var { data } = await this.$axios.put(
        "orders/" + this.changeOrderData.order_id,
        this.changeOrderData
      );
      if (data.meta.status == 201) {
        this.$message({
          type: "success",
          message: data.meta.msg,
        });
        this.dialogChangeOrders = false;
        this.getOrders();
      } else {
        this.$message({
          type: "error",
          message: data.meta.msg,
        });
      }
    },
    // 查询详情
    QueryOrders(orderinfo) {
      this.dialogqueryOrders = true;
      this.QueryOrdersData = orderinfo;
    },
  },
  mounted() {
    this.getOrders();
  },
};
</script>

<style>
.el-dialog__header {
  line-height: 0px;
}
.dialog-footer {
  line-height: 0px;
}
.has-gutter {
  line-height: 35px;
}
.el-row {
  line-height: 60px;
}
.el-card__body {
  line-height: 0px;
}
.serch {
  width: 22vw !important;
}
</style>